<?php
namespace app\mobile\controller;

use app\model\Theme; 
use app\model\Fans; 

class Sharepyq extends Base
{
    public function Sharepyq()
    {
global $_G, $_P;
$op = !empty($_P['op']) ? $_P['op'] : 'display';
$system=$_G['system'];
$fans = $this->checkuser();
$themeid = intval($_P['themeid']);
if($themeid < 0){
	return_msg('活动入口不正确');
}
$recordid = intval($_P['recordid']);
$theme = Theme::getThemeById($themeid);
$config = Theme::getConfig($themeid);
$workerid = intval($_P['workerid']);
$type = intval($_P['type']);
$pw = array(":workerid"=>$workerid);
$p = array(":themeid"=>$themeid);
$pm = array(":themeid"=>$themeid,":openid"=>$_G['openid']);
if($op == 'display'){

	$share = db_find("SELECT * FROM ".tablename(t_share)."
			WHERE themeid =:themeid and openid = :openid and status = 1 order by id desc limit 1",$pm);
	$begintime1 = $theme['begintime'];
	$endime1 = $theme['endtime'];

	if($endime1 < TIMESTAMP ||  $theme['status'] != 1){
		$result['errcode'] = 1;
		$result['msg'] = '活动已经结束啦，感谢您的支持';
		return_msg($result, '', 'ajax');
	}

	db_query("UPDATE ".tablename(t_theme)." SET forward = forward +1 WHERE id =:themeid",$p);
	db_query("UPDATE ".tablename(t_report)." SET forward = forward +1 WHERE software = '".MODULE_NAME."' and themeid =:themeid and riqi ='" . date("Y-m-d") ."'",$p);
	db_query("UPDATE ".tablename(t_fans)." SET forward = forward +1 WHERE themeid =:themeid and openid = :openid",$pm);
	if (!empty($workerid)){
		db_query("UPDATE ".tablename(t_worker)." SET forward = forward +1 WHERE themeid = " . $themeid . " and uid =:workerid",$pw);
	}
	Theme::workerreport($themeid,$workerid,array('forward' =>1));

	// if (!empty($workerid)){
	// 	db_query("UPDATE ".tablename(t_worker)." SET forward = forward +1 WHERE themeid = " . $themeid . " and uid =:workerid",$pw);
	// }
	// if (empty($type)) {
	// 	$result['errcode'] = 1;
	// 	$result['msg'] = '活动尚未开始，敬请期待~';
	// 	return_msg($result, '', 'ajax');
	// }

	$fromuid = intval($_P['fromuid']);
	$fopenid = $_P['fopenid'];
	if($fromuid == $_G['member']['uid'] || $fopenid == $_G['openid']){
		$fromuid = 0;
		$fopenid = "";
	}
	
	$data = array(
			'uniacid'  => $_G['mpid'],
			'themeid'  => $themeid,
			'openid'   => $_G['openid'],
			'uid'	   => $_G['member']['uid'],
			'addtime'  => TIMESTAMP,
			'status'   => 0,
			'workerid'  => $workerid,
			'fromuid'  => $fromuid,
			'fopenid'  => $fopenid,
	);
	
    if ($system['hdget']==1) {
        if (Fans::getuid_old($fromuid,$themeid)) {
            $data['fromuid_old']  = $fromuid;
            $data['fopenid_old']  = $fopenid;
        }
        if (Fans::getuid_old($workerid,$themeid)) {
            $data['workerid_old']  = $workerid;
        }
    }

	$yaoyiyao = floatval(db_fetchcolumn ( 'SELECT sum(fee) FROM ' . tablename ( t_commshare ) . "
				WHERE themeid = '{$themeid}' and status <2 and uid ='{$_G['member']['uid']}'" ));

	$todaylight = intval(db_fetchcolumn("SELECT COUNT(*) FROM ".tablename(t_share)."
			WHERE themeid =:themeid and openid = :openid and status = 1 and addtime > " . strtotime(date('Y-m-d')),$pm));

	if(($yaoyiyao>0 || $config['isicon'] > 0 || $config['fanpaigroupid']>0) && $todaylight < $config['todayicon']){
		$data['status'] = 1;
	}
	
	$insertid = db_insert(t_share,$data);
	
	//翻牌抽奖，发朋友圈增加抽奖次数
    if ($system['fanpai']==1 && $config['fanpaigroupid']>0 ) {

    	$todayshare = intval(db_fetchcolumn("SELECT COUNT(*) FROM ".tablename(t_fanpaijihui)."
			WHERE themeid= " . $themeid . " and  uid= " . $_G['member']['uid'] . " and type = 3 and addtime > " . strtotime(date('Y-m-d'))));
    	if ($todayshare == 0){
	        $sql = "SELECT * FROM " . tablename ( t_fanpaijihui ) . " WHERE themeid= " . $themeid . " and  uid= " . $_G['member']['uid'] . " and type = 3";
	        $jihuilist = db_select ( $sql );

	       	if (count($jihuilist)==0) {
	       		$jihui = $config['fanpai1day'];
	       	} elseif (count($jihuilist)==1) {
	       		$jihui = $config['fanpai2day'];
	       	} elseif (count($jihuilist)==2) {
	       		$jihui = $config['fanpai3day'];
	       	}
	       	$isorder = db_find ( "SELECT * FROM " . tablename (t_order) . " WHERE themeid = '{$themeid}' and uid = '{$_G['member']['uid']}' and paystate = 1 and orderfee>0 order by id desc" );

	       	if ($config['fanpaipengyouquan']==0 && !empty($isorder) || $config['fanpaipengyouquan']==1){
		        if ($jihui > 0){
		            $tempdata = array(
		                'themeid'   => $themeid,
		                'uniacid'   => $_G['mpid'],
		                'uid'       => $_G['member']['uid'],
		                'openid'    => $_G['openid'],
			            'nickname'  => $fans['nickname'],
			            'avatar'    => empty($fans['avatar']) ? $fans['headimgurl']:$fans['avatar'],
		                'status'    => 0,
		                'type'    => 3,
		                'jihui'    => $jihui,
		                'addtime'   => TIMESTAMP,
		                'workerid' => $workerid,
		            );
				    if ($system['hdget']==1 &&Fans::getuid_old($workerid,$themeid)) {
				        $data['workerid_old']  = $workerid;
				    }
		            $insertid = db_insert(t_fanpaijihui, $tempdata);
		            db_query("UPDATE ".tablename(t_fans)." SET jihui = jihui + " . $jihui . " WHERE themeid = " . $themeid . " and uid = ".$_G['member']['uid'] ."");
		        }
		    }
		}

	}
    $lightsum = 0;
    if($config['iconnum2'] <= 0){
        $lightsum = intval(db_fetchcolumn("SELECT COUNT(*) FROM ".tablename(t_share)."
				WHERE themeid = :themeid and openid = :openid and status = 1",$pm));
    }else{
        $sql = "select count(*) from (SELECT COUNT(*) FROM ".tablename(t_scan)."
				WHERE themeid = :themeid and fopenid = :openid and status = 1 group by openid) a";
        $lightsum = intval(db_fetchcolumn($sql,$pm));
        $lightsum = intval($lightsum/$config['iconnum2']);
    }
	if($config['isicon'] > 0 && $data['status'] == 1){
		$ishasinfo = 1;
		$p3 = array(":openid"=>$_G['openid'],":themeid"=>$themeid);
		$recordinfo = db_find ( "SELECT * FROM " . tablename (t_record) . "
		                                WHERE themeid =:themeid
	                                        and openid = :openid  order by id desc limit 1",$p3);
		if (empty($recordinfo)) {
			if ($config['comm4ziliao']==1 || $config['isicon'] > 0 && $config['iconziliao']!=1){
				$ishasinfo = 0;
			}
		}
		$result['errcode'] = 0;
		$result['ishasinfo'] = $ishasinfo;
			
	}else{
		$result['errcode'] = 1;
		$result['ishasinfo'] = 1;
	}
		$result['lightsum'] = $lightsum;
		if ($config['iconlogo']!=1) {
			$result['lighticon'] = tomedia($config['lighticon']);
		}else{
			$shoplist = db_select ( "SELECT * FROM " . tablename (t_shangjia) .
					" WHERE  state = 1 and darkicon<>'' order by displayorder" );
			$iconnum = count($shoplist);

			if($lightsum >= $iconnum){
				$lightsum = $iconnum;
			}

			$lighticon = tomedia($shoplist[$lightsum-1]['logo']);
			$lighticon = str_replace("/99.jpg","/logo.jpg",$lighticon);
			$result['lighticon'] = $lighticon;
			
		}
		$result['msg'] = '感谢支持!';
		return_msg($result, '', 'ajax');
	
}






}
}